<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, SunWeber.Com.
 * @license			http://www.sunweber.com/license.htm
 * @link				http://www.sunweber.com/
 * @since				Version 1.0 Beta
 * 
 */

include( './admin/model/article.class.php' );
$m_article = new Article();


if( $action == 'new' )
{
	if( $do == 'newarticle' )
	{
		$cat = get_post( 'cat' );
		$time = date( 'Y-m-d H:i:s' );
		$category = $m_article->get_category( true );
		if( empty( $category ) )
		{
			jump( '系统提示：文档分类不存在，请先添加分类！', SCRIPT_NAME.'?m=category&a=new&do=newarcat', 2 );
			exit;
		}
		if( empty( $cat ) )
		{
			include( display( 'select_category' ) );
			$outputs = ob_get_contents();
			echo ob_contents( $outputs );
			exit;
		}
		$from_list = $m_article->get_from_list();
		$author_list = $m_article->get_author_list();
		$province = $m_article->get_area( 'province' );
		include( display( 'article_new' ) );
		unset( $cat, $time, $column_tree, $special, $author, $from );
	}
	elseif( $do == 'addarticle' )
	{
		$datas = get_post( 'datas' );
		$article_id = $m_article->add_article( $datas );
		if( $article_id )
		{
			$success = 'SunCMS提示：文档发布成功！ <a href="'.SCRIPT_NAME.'?m=article&a=new&do=newmore&id='.$article_id.'">续发本文档</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=new&do=newarticle&cat='.$datas['cid'].'">发布新文章</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=mag&do=list">返回文档列表</a>
						<a href="article.php?id='.$article_id.'">查看本文档</a>';
			jump( $success, SCRIPT_NAME.'?m=article&a=new&do=newarticle&cat='.$datas['cid'] );
		}
		else
		{
			alert( '文档发布失败，请联系管理员！' );
			go_back();
		}
		unset( $datas, $result, $success );
	}
	elseif( $do == 'newmore' )
	{
		$id = get_post( 'id' );
		$datas = $m_article->get_article_info( $id );
		include( display( 'article_new_more' ) );
		unset( $id, $title );
	}
	elseif( $do == 'addmore' )
	{
		$id = get_post( 'id' );
		$datas = get_post( 'datas' );
		if( $m_article->add_more( $id, $datas ) )
		{
			$success = 'SunCMS提示：文档续发成功！ <a href="'.SCRIPT_NAME.'?m=article&a=add&do=addmorep&id='.$id.'">续发本文档</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=add&dp=addarp">发布新文档</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=mag&do=list&tp='.$member.'">返回文档列表</a>
						<a href="article.php?id='.$id.'">查看本文档</a>';
			jump( $success, SCRIPT_NAME.'?m=article&a=index' );
		}
		else
		{
			alert( '文档续发失败，请联系管理员！' );
			go_back();
		}
		unset( $datas, $success );
	}
}
elseif( $action == 'edit' )
{
	if( $do == 'editcheck' )
	{
		$id = get_post( 'id' );
		$article_info = $m_article->get_article_info( $id );
		echo $article_info['page_no'] > 1?1:0;
	}
	if( $do == 'editarticle' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );//0代表完整文章编辑模式，1代表只编辑第一页
		$article_info = $m_article->get_article_info( $id, true );
		$title = $article_info['title'];
		$page_no = $article_info['page_no'];
		//如果只有一页/全文编辑模式，则直接编辑
		if( $article_info['page_no'] == 1 || $page == 'all' || $page == 1 )
		{
			$page = ( $article_info['page_no'] == 1 || $page == 1 )?1:0;
			$category = $m_article->get_category();
			$from_list = $m_article->get_from_list();
			$author_list = $m_article->get_author_list();
			$province = $m_article->get_area( 'province' );
			$datas = $m_article->get_edit_article( $id, $page, true );
			$datas['province'] != '' && $city = $m_article->get_area( 'city', $datas['province'] );
			include display( 'article_edit' );
			$outputs = ob_get_contents();
			echo ob_contents( $outputs );
			unset( $id, $page, $type, $article_info, $category, $from, $author, $datas );
			exit;
		}
		//分页编辑模式
		elseif( !empty( $page ) )
		{
			$content = $m_article->get_edit_article( $id, $page );
			include display( 'article_edit_more' );
			$outputs = ob_get_contents();
			echo ob_contents( $outputs );
			unset( $id, $edit_page, $type, $page_ar, $page_no, $title, $content );
			exit;
		}
		//显示分页/编辑模式
		$page = $article_info['page'];
		include( display( 'select_edit' ) );
		unset( $id, $edit_page, $type, $page_ar, $page_no, $title );
	}
	elseif( $do == 'updatearticle' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );
		$datas = get_post( 'datas' );
		$type = get_post( 'type' ); $type = $type == 'pm'?0:1;//pm:分页模式
		$result = $m_article->update_article( $id, $page, $datas, $type );
		if( $result )
		{
			$success = 'SunCMS提示：文档修改成功！ <a href="'.SCRIPT_NAME.'?m=article&a=edit&do=editarticle&id='.$id.'&page='.$page.'">继续修改本文档</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=new&do=newarticle&cat='.$datas['cid'].'">发布本类新文章</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=mag&do=list">返回文档列表</a>
						<a href="article.php?id='.$id.'">查看本文档</a>';
			jump( $success, SCRIPT_NAME.'?m=article&a=new&do=newarticle&cat='.$id );
		}
		else
		{
			alert( '文档修改失败，请联系管理员！' );
			go_back();
		}
		unset( $datas, $result, $success );
	}
}
elseif( $action == 'mag' )
{
	if( $do == 'list' )
	{
		$type = get_post( 'type' ); $type = empty( $type )?'all':$type;
		$page = get_post( 'page' ); $page = empty( $page )?1:$page;
		switch( $type )
		{
			case 'draft':
				$id = 0;
				$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=draft';
				break;
			case 'cat':
				$id = get_post( 'id' );
				$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=cat&id='.$id;
				break;
			case 'vali':
				$id = 0;
				$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=vali';
				break;
			default:
				$id = 0;
				$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=all';
		}
		$article_no = $m_article->get_article_no( $type, $id );
		$page_no = ceil( $article_no/15 );
		$articles = $m_article->get_article_list( ( $page-1 )*15, $type, $id );
		include display( $type == 'vali'?'article_validate':'article_list' );
		unset( $type, $id, $url, $page, $article_no, $articles, $category );
	}
	elseif( $do == 'delpage' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );
		$m_article->del_page( $id, $page );
		go_url( SCRIPT_NAME.'?m=article&a=edit&do=editarticle&id='.$id );
		unset( $id, $page );
	}
	elseif( $do == 'newcopy' || $do == 'newmove' )
	{
		$ids = get_post( 'ids' );
		$category = $m_article->get_category();
		include( display( $do == 'newcopy'?'article_copy':'article_move' ) );
		unset( $ids, $category );
	}
	elseif( $do == 'copy' )
	{
		$ids = get_post( 'ids' );
		$cids = get_post( 'cids' );
		echo $m_article->copy( $ids, $cids )?1:0;
	}
	elseif( $do == 'move' )
	{
		$ids = get_post( 'ids' );
		$cid = get_post( 'cid' );
		echo $m_article->move( $ids, $cid )?1:0;
	}
	elseif( $do == 'delarticle' )
	{
		$ids = get_post( 'ids' );
		echo $m_article->del_article( $ids )?1:0;
	}
}
elseif( $action == 'getcity' )
{
	$pid = get_post( 'pid' );
	$city = $m_article->get_area( 'city', $pid );
	echo json_encode( $city );
}























//文章列表
if( $action == 'index' )
{
	$type = get_post( 'type' );
	$page = get_post( 'page' );
	$type = empty( $type )?'all':$type;
	$page = empty( $page )?1:$page;
	switch( $type )
	{
		case 'recycle':
			$get_type = 'recycle';
			$id = 0;
			$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=recycle';
			break;
		case 'draft':
			$get_type = 'draft';
			$id = 0;
			$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=draft';
			break;
		case 'col':
			$get_type = 'col';
			$id = get_post( 'id' );
			$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=col&id='.$id;
			break;
		case 'vali':
			$get_type = 'vali';
			$id = 0;
			$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=vali';
			break;
		default:
			$get_type = 'all';
			$id = 0;
			$url = SCRIPT_NAME.'?m=article&a=mag&do=list&type=all';
	}
	$article_no = $m_article->get_article_no( $get_type, $id );
	include( SUN_CLASS.'pagination.php' );
	$page_config = array
	(
			'base_url' => $url,
			'cur_page' => $page,
			'num_pages' => ceil( $article_no/15 )
	);
	$pagination = new Pagination( $page_config );
	$page_link = $pagination->create_links();
	$articles = $m_article->get_article_list( ( $page-1 )*15, $get_type, $id );
	$column_tree = $m_article->get_column_tree();
	include display( $type == 'vali'?'ar_validate':'article_list' );
	unset( $type, $page, $article_no, $page_link, $articles );
}
//搜索文章
elseif( $action == 'search' )
{
	$id = 0;
	$rule = get_post( 'rule' );
	$key = get_post( 'key' );
	$page = get_post( 'page' );
	$page = empty( $page )?1:$page;
	$result = $m_article->search_article( ( $page-1 )*20, strim( $rule ), $key );
	$article_no = $result['articleNo'];
	$articles = $result['articles'];
	$page_no = ceil( $article_no/20 );
	include( SUN_CLASS.'pagination.php' );
	$page_config = array
	(
			'base_url' => SCRIPT_NAME.'?m=article&a=search&rule='.$rule.'&key='.$key,
			'cur_page' => $page,
			'num_pages' => $page_no
	);
	$pagination = new Pagination( $page_config );
	$page_link = $pagination->create_links();
	$column_tree = $m_article->get_column_tree();
	$title = '筛选结果';
	$get_type = 'all';
	include display( 'ar_list' );
	unset( $id, $rule, $key, $page, $results, $article_no, $articles, $page_no, $page_config, $pagination, $page_link, $column_tree, $title );
}
//添加文章
elseif( $action == 'newh' )
{
	if( $do == 'newarp' )
	{
		$col = get_post( 'col' );
		$time = date( 'Y-m-d H:i:s' );
		$column_tree = $m_article->get_column_tree( true );
		if( empty( $column_tree ) )
		{
			jump( '系统提示：栏目不存在，请先添加栏目！', SCRIPT_NAME.'?m=arcolumn&a=add&do=addcolp', 2 );
			exit;
		}
		if( empty( $col ) )
		{
			include( display( 'select_category' ) );
			exit;
		}
		$special = $m_article->get_special_list();
		$from = $m_article->get_from_list();
		$author = $m_article->get_author_list();
		include display( 'new_article' );
		unset( $col, $time, $column_tree, $special, $author, $from );
	}
	elseif( $do == 'addar' )
	{
		$data_array = get_post( 'data' );
		$data_array = json_decode( stripslashes( $data_array ), true );
		$ar_basic = $data_array['ar_basic'];
		$ar_content = $data_array['ar_content'];
		$result = $m_article->add_article( strim( $ar_basic ), strim( $ar_content ) );
		if( $result )
		{
			$res_array = array
			(
					'ok' => 1,
					'aid' => $result['id'],
					'tags' => $result['tags'],
					'keywords' => $result['keywords'],
					'description' => $result['description'],
			);
			echo json_encode( $res_array );
		}
		else
		{
			echo "{ok:0}";
		}
		unset( $data_array, $ar_basic, $ar_content, $result, $res_array );
	}
	elseif( $do == 'addmorep' )
	{
		$id = get_post( 'id' );
		$title = $m_article->get_article_title( $id );
		include display( 'ar_add_more' );
		unset( $id );
	}
	elseif( $do == 'addmore' )
	{
		$id = get_post( 'id' );
		$down_pic = get_post( 'down_pic' );
		$datas = array();
		$datas['down_pic'] = empty( $down_pic )?0:1;
		$datas['content'] = get_post( 'content' );
		if( $m_article->add_more( strim( $datas ), $id ) )
		{
			$success = '<a href="'.SCRIPT_NAME.'?m=article&a=add&do=addmorep&id='.$id.'">续发本文章</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=add&dp=addarp">发布新文章</a>
						<a href="'.SCRIPT_NAME.'?m=article&a=mag&do=list&tp='.$member.'">返回文章列表</a>
						<a href="article.php?id='.$id.'">查看本文章</a>';
			jump( $success, SCRIPT_NAME.'?m=article&a=index' );
		}
		else
		{
			alert( '文章续发失败，请联系管理员！' );
			go_back();
		}
		unset( $id, $datas, $down_pic );
	}
}
//编辑文章
elseif( $action == 'edit' )
{
	if( $do == 'edit' )
	{
		$id = get_post( 'id' );
		$edit_page = get_post( 'page' );
		$type = get_post( 'type' );
		$page_ar = $m_article->get_article_page( $id );
		$page_no = count( $page_ar );
		//如果只有一页/全文编辑模式，则直接编辑
		if( $page_no == 1 || $type == 'all' || $edit_page == 1 )
		{
			$page = ( $edit_page == 1 || $page_no == 1 )?1:0;
			$column_tree = $m_article->get_column_tree();
			$special = $m_article->get_special_list();
			$from = $m_article->get_from_list();
			$author = $m_article->get_author_list();
			$result = $m_article->get_article( $id, $page, true );
			$ar_basic = $result['ar_basic'];
			$ar_content = $result['ar_content'];
			include display( 'ar_edit_article' );
			unset( $id, $edit_page, $type, $page_ar, $page_no, $page, $column_tree, $special, $from, $author, $result, $ar_basic, $ar_content );
			exit;
		}
		//分页编辑模式
		if( !empty( $edit_page ) )
		{
			$title = $m_article->get_article_title( $id );
			$content = $m_article->get_article( $id, $edit_page );
			include display( 'ar_edit_cont' );
			unset( $id, $edit_page, $type, $page_ar, $page_no, $title, $content );
			exit;
		}
		//显示分页/编辑模式
		$title = $m_article->get_article_title( $id );
		include( display( 'ar_page_list' ) );
		unset( $id, $edit_page, $type, $page_ar, $page_no, $title );
	}
	elseif( $do == 'updatear' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );
		$data_array = get_post( 'data' );
		$data_array = json_decode( stripslashes( $data_array ), true );
		$ar_basic = $data_array['ar_basic'];
		$ar_content = $data_array['ar_content'];
		$result = $m_article->update_ar( $id, strim( $ar_basic ), strim( $ar_content ), $page );
		if( $result )
		{
			$res_array = array
			(
					'ok' => 1,
					'aid' => $id,
					'tags' => $result['tags'],
					'keywords' => $result['keywords'],
					'description' => $result['description'],
			);
			echo json_encode( $res_array );
		}
		else
		{
			echo "{ok:0}";
		}
		unset( $id, $page, $data_array, $ar_basic, $ar_content, $result, $res_array );
	}
	elseif( $do == 'updatearc' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );
		$data = get_post( 'data' );
		$datas = json_decode( stripslashes( $data ),true );
		echo $m_article->l( $id, $page, strim( $datas ) )?"{'ok':1}":"{'ok':0}";
		unset( $id, $page, $data, $datas );
	}
}
//检查标题重复
elseif( $action == 'checktitle' )
{
	$data = get_post( 'title' );
	echo $m_article->check_title( strim( $data ) )?"{'ok':0}":"{'ok':1}";
	unset( $data );
}
//删除文章页面
elseif( $action == 'delpage' )
{
	$id = get_post( 'id' );
	$pg = get_post( 'pg' );
	if( $m_article->del_page( $id, $pg ) )
	{
		go_url( SCRIPT_NAME.'?m=article&a=edit&id='.$id );
	}
	else 
	{
		alert( '页面删除失败，请联系管理员！' );
		go_back();
	}
	unset( $id, $pg );
}
elseif( $action == 'delete' || $action == 'recycle' || $action == 'post' || $action == 'restore' || $action == 'recommend' || $action == 'unrecommend' )
{
	switch( $action )
	{
		case 'delete':
			$success = '文章删除成功';
			$error = '文章删除失败';
			break;
		case 'recycle':
			$success = '文章已移入回收站';
			$error = '文章回收失败';
			break;
		case 'post':
			$success = '文章发布成功';
			$error = '文章发布失败';
			break;
		case 'restore':
			$success = '文章还原成功';
			$error = '文章还原失败';
			break;
		case 'recommend':
			$success = '文章推荐成功';
			$error = '文章推荐失败';
			break;
		case 'unrecommend':
			$success = '文章取消推荐成功';
			$error = '文章取消推荐失败';
			break;
	}
	$ids = get_post( 'ids' );
	if( $m_article->$action( $ids ) )
	{
		jump( '系统提示：'.$success.'，页面跳转中。。。', $referer, 1 );
	}
	else
	{
		alert( $error.'，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'move' )
{
	$ids = get_post( 'ids' );
	$column_id = get_post( 'cid' );
	if( $m_article->move( $ids, $column_id ) )
	{
		jump( '系统提示：文章移动成功，页面跳转中。。。', $referer, 1 );
	}
	else
	{
		alert( '文章移动失败，请联系管理员！' );
		go_back();
	}
}
elseif( $action == 'validate' )
{
	if( $do == 'index' )
	{
		$id = get_post( 'id' );
		$page = get_post( 'page' );
		$page = $page?$page:1;
		$article = $m_article->get_article_one( $id, $page );
		$article_no = $article['page'];
		include( SUN_CLASS.'pagination.php' );
		$page_config = array
		(
				'base_url' => SCRIPT_NAME.'?m=article&a=validate&do=index&id='.$id,
				'cur_page' => $page,
				'num_pages' => $article_no
		);
		$pagination = new Pagination( $page_config );
		$page_link = $pagination->create_links();
		include display( 'ar_article' );
		unset( $id, $article );
	}
	elseif( $do == 'validate' )
	{
		$id = get_post( 'id' );
		$datas['validate'] = get_post( 'validate' );
		$datas['setting'] = get_post( 'setting' );
		$datas['proposal'] = get_post( 'proposal' );
		$datas['title'] = get_post( 'title' );
		if( $m_article->article_validate( $id, strim( $datas ) ) )
		{
			jump( '系统提示：文章审核成功，页面跳转中。。。', SCRIPT_NAME.'?m=article&a=mag&do=list&type=vali', 1 );
		}
		else
		{
			alert( '文章审核失败，请联系管理员！' );
			go_back();
		}
	}
	elseif( $do == 'btvalidate' )
	{
		$type = get_post( 'tp' );
		$type = ( $type == 'validate' )?1:0;
		$ids = get_post( 'ids' );
		echo $m_article->validate( $ids, $type )?"{'ok':1}":"{'ok':0}";
	}
}
elseif( $action == 'comment' )
{
	if( $do == 'index' )
	{
		$page = get_post( 'page' );
		$page = empty( $page )?1:$page;
		$type = get_post( 'type' );
		if( $type == 'search' )
		{
			$rule = get_post( 'rule' );
			$value = get_post( 'value' );
			$comment = $m_article->get_comment( $rule, ( $page-1 )*20, $value );
			$comment_no = $m_article->get_comment_no( $rule, $value );
		}
		else 
		{
			$comment = $m_article->get_comment( 'unvalidate', ( $page-1 )*20 );
			$comment_no = $m_article->get_comment_no( 'unvalidate' );
		}
		include( SUN_CLASS.'pagination.php' );
		$page_config = array
		(
				'base_url' => $url,
				'cur_page' => $page,
				'num_pages' => ceil( $comment_no/20 )
		);
		$pagination = new Pagination( $page_config );
		$page_link = $pagination->create_links();
		include display( 'comment' );
	}
	elseif( $do == 'validate' )
	{
		$type = get_post( 'type' );
		$ids = get_post( 'ids' );
		if( $m_article->validate_comment( $type, $ids ) )
		{
			jump( 'SunCMS提示：回复审核成功！', $referer, 1 );
		}
		else 
		{
			alert( '回复审核失败，请联系管理员！' );
			go_back();
		}
	}
}

unset( $m_article );